<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.example.domain.User" %>
<%@ page import="java.util.List" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - 论坛系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 100%);
            min-height: 100vh;
            color: #374151;
        }

        .header {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 16px;
        }

        .header h1 {
            font-size: 28px;
            font-weight: bold;
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .back-btn {
            background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 25px;
            text-decoration: none;
            font-weight: 600;
            transition: all 0.3s ease;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .back-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(107, 114, 128, 0.4);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 30px 20px;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 24px;
            margin-bottom: 40px;
        }

        .stat-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            padding: 30px;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.08);
            text-align: center;
            border: 1px solid rgba(255, 255, 255, 0.2);
            transition: all 0.3s ease;
        }

        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.12);
        }

        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            margin: 0 auto 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: white;
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
        }

        .stat-number {
            font-size: 36px;
            font-weight: bold;
            background: linear-gradient(135deg, #1f2937 0%, #3b82f6 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            margin-bottom: 8px;
        }

        .stat-label {
            color: #6b7280;
            font-size: 16px;
            font-weight: 500;
        }

        .table-container {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }

        .table-header {
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .table-header h2 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
        }

        .table-header p {
            opacity: 0.9;
            font-size: 16px;
        }

        .table-wrapper {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 20px 16px;
            text-align: left;
            border-bottom: 1px solid rgba(229, 231, 235, 0.5);
        }

        th {
            background: rgba(248, 250, 252, 0.8);
            font-weight: 600;
            color: #374151;
            font-size: 14px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        td {
            color: #4b5563;
            font-size: 15px;
        }

        tr:hover {
            background: rgba(248, 250, 252, 0.5);
        }

        .status-badge, .role-badge {
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .status-active {
            background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
            color: #065f46;
        }

        .status-blocked {
            background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%);
            color: #991b1b;
        }

        .role-admin {
            background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
            color: #92400e;
        }

        .role-user {
            background: linear-gradient(135deg, #e0e7ff 0%, #c7d2fe 100%);
            color: #3730a3;
        }

        .action-form {
            display: flex;
            align-items: center;
            gap: 12px;
            flex-wrap: wrap;
        }

        .action-select {
            padding: 10px 16px;
            border: 2px solid #e5e7eb;
            border-radius: 12px;
            font-size: 14px;
            background: white;
            transition: all 0.3s ease;
            min-width: 120px;
        }

        .action-select:focus {
            outline: none;
            border-color: #3b82f6;
            box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
        }

        .action-btn {
            padding: 10px 20px;
            background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
            color: white;
            border: none;
            border-radius: 12px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(59, 130, 246, 0.4);
        }

        .empty-state {
            text-align: center;
            padding: 80px 20px;
            color: #6b7280;
        }

        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 24px;
            opacity: 0.5;
        }

        .empty-state h3 {
            font-size: 24px;
            color: #374151;
            margin-bottom: 12px;
        }

        .empty-state p {
            font-size: 16px;
            line-height: 1.6;
        }

        @media (max-width: 768px) {
            .header-content {
                flex-direction: column;
                text-align: center;
            }

            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
                gap: 16px;
            }

            .stat-card {
                padding: 20px;
            }

            .action-form {
                flex-direction: column;
                gap: 8px;
            }

            .action-select,
            .action-btn {
                width: 100%;
            }

            th, td {
                padding: 12px 8px;
                font-size: 13px;
            }
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px 16px;
            }

            .stats-grid {
                grid-template-columns: 1fr;
            }

            .table-header {
                padding: 20px;
            }

            .table-header h2 {
                font-size: 20px;
            }
        }
    </style>
</head>
<body>
<div class="header">
    <div class="header-content">
        <h1>
            <span>👥</span>
            <span>用户管理</span>
        </h1>
        <a href="/admin" class="back-btn">
            <span>🏠</span>
            <span>返回管理首页</span>
        </a>
    </div>
</div>

<div class="container">
    <%
        List<User> users = (List<User>) request.getAttribute("users");
        int totalUsers = users != null ? users.size() : 0;
        int activeUsers = 0;
        int blockedUsers = 0;
        int adminUsers = 0;

        if (users != null) {
            for (User user : users) {
                if ("active".equals(user.getStatus())) activeUsers++;
                if ("blocked".equals(user.getStatus())) blockedUsers++;
                if ("admin".equals(user.getRole())) adminUsers++;
            }
        }
    %>

    <div class="stats-grid">
        <div class="stat-card">
            <div class="stat-icon">👤</div>
            <div class="stat-number"><%= totalUsers %></div>
            <div class="stat-label">总用户数</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">✅</div>
            <div class="stat-number"><%= activeUsers %></div>
            <div class="stat-label">活跃用户</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">🚫</div>
            <div class="stat-number"><%= blockedUsers %></div>
            <div class="stat-label">封禁用户</div>
        </div>
        <div class="stat-card">
            <div class="stat-icon">👑</div>
            <div class="stat-number"><%= adminUsers %></div>
            <div class="stat-label">管理员</div>
        </div>
    </div>

    <div class="table-container">
        <div class="table-header">
            <h2>用户列表</h2>
            <p>管理所有注册用户的状态和权限</p>
        </div>

        <% if (users != null && !users.isEmpty()) { %>
        <div class="table-wrapper">
            <table>
                <thead>
                <tr>
                    <th>ID</th>
                    <th>用户名</th>
                    <th>邮箱</th>
                    <th>注册时间</th>
                    <th>角色</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <% for (User user : users) { %>
                <tr>
                    <td><%= user.getId() %></td>
                    <td><%= user.getUsername() %></td>
                    <td><%= user.getEmail() %></td>
                    <td><%= user.getRegisterTime() %></td>
                    <td>
                        <span class="role-badge role-<%= user.getRole() %>">
                            <%= "admin".equals(user.getRole()) ? "管理员" : "普通用户" %>
                        </span>
                    </td>
                    <td>
                        <span class="status-badge status-<%= user.getStatus() %>">
                            <%= "active".equals(user.getStatus()) ? "正常" : "封禁" %>
                        </span>
                    </td>
                    <td>
                        <form action="/admin/updateUserStatus" method="post" class="action-form">
                            <input type="hidden" name="id" value="<%= user.getId() %>">
                            <select name="status" class="action-select">
                                <option value="active" <%= "active".equals(user.getStatus()) ? "selected" : "" %>>激活</option>
                                <option value="blocked" <%= "blocked".equals(user.getStatus()) ? "selected" : "" %>>封禁</option>
                            </select>
                            <button type="submit" class="action-btn">更新状态</button>
                        </form>
                    </td>
                </tr>
                <% } %>
                </tbody>
            </table>
        </div>
        <% } else { %>
        <div class="empty-state">
            <div class="empty-state-icon">👤</div>
            <h3>暂无用户数据</h3>
            <p>系统中还没有注册用户</p>
        </div>
        <% } %>
    </div>
</div>
</body>
</html>
